package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupVo;
import com.atguigu.gulimall.product.vo.ProductGroup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private CategoryDao categoryDao;
    @Resource
    private AttrGroupDao attrGroupDao;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatId(Map<String, Object> params, Integer catId) {

        if(catId == 0){
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
            if(!StringUtils.isEmpty(key)){
                queryWrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
            }
            return new PageUtils(this.page(
                    new Query<AttrGroupEntity>().getPage(params),queryWrapper
            ));
        }else {
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catId);
            String key = (String) params.get("key");
            if(!StringUtils.isEmpty(key)){
                queryWrapper.and(wrapper->{
                    wrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }

    }

    @Override
    public AttrGroupEntity getByIdAndCatelongPath(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = this.baseMapper.selectById(attrGroupId);
        //查出级联id
        List<Long> catelogPath = new ArrayList<>();

        attrGroupEntity.setCatelogPath(getCatelogPath(attrGroupEntity.getCatelogId(),catelogPath));
        return attrGroupEntity;
    }


    public List<Long> getCatelogPath(Long catelogId,List<Long> catelogPath) {

        catelogPath.add(catelogId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if(categoryEntity.getParentCid() != 0){
            getCatelogPath(categoryEntity.getParentCid(),catelogPath);
        }
        Collections.reverse(catelogPath);
        return catelogPath;
    }

    @Override
    public List<AttrGroupVo> getAttrgroupAndAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> catelog_id = attrGroupService.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupVo> attr_group_id1 = catelog_id.stream().map(attrGroupEntity -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupVo);
            List<Long> attr_group_id = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupEntity.getAttrGroupId())).stream()
                    .map(attrAttrgroupRelationEntity -> {
                        return attrAttrgroupRelationEntity.getAttrId();
                    }).collect(Collectors.toList());
            if(attr_group_id!=null && attr_group_id.size()!=0){
                List<AttrEntity> attrEntities = attrService.listByIds(attr_group_id);
                attrGroupVo.setAttrs(attrEntities);
            }
            return attrGroupVo;
        }).collect(Collectors.toList());
        return attr_group_id1;
    }

    @Override
    public List<ProductGroup> getProductGroups(Long spuId, Long calalogId) {
        List<ProductGroup> productGroupList = this.baseMapper.getProductGroups(spuId,calalogId);
        return productGroupList;
    }


}